﻿#ifndef PST_TDSPDEFAULTINTERACTORSTYLE_H
#define PST_TDSPDEFAULTINTERACTORSTYLE_H

#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkSmartPointer.h>
#include <QWidget>
#include <QMenu>
#include <QCursor>
#include <QPoint>
#include <vtkPropPicker.h>
#include <vtkObjectFactory.h>
#include <vtkRenderWindowInteractor.h>
#include <QObject>
#include <vtkActor.h>

namespace pst
{
    class TDSPDefaultInteractorStyle :public QObject, public vtkInteractorStyleTrackballCamera
    {
        Q_OBJECT
    public:
        static TDSPDefaultInteractorStyle* New();
        vtkTypeMacro(TDSPDefaultInteractorStyle, vtkInteractorStyleTrackballCamera);

        // 用于设置用于计算全局坐标的 QWidget，一般为 QVTKWidget 或 QVTKOpenGLNativeWidget
        void SetRenderWindowWidget(QWidget* widget) { this->RenderWindowWidget = widget; }

        // 0 左键按住是旋转，1 左键按住是平移
        void setInnerInteractionStyle(int style)
        {
            m_interactionStyle = style;
        }

        virtual void OnRightButtonUp() override
        {
            // 获取当前鼠标位置（像素坐标，原点在窗口左下角）
            int* eventPos = this->GetInteractor()->GetEventPosition();

            // 使用 vtkPropPicker 检查点击处是否在 actor 上
            vtkSmartPointer<vtkPropPicker> picker = vtkSmartPointer<vtkPropPicker>::New();
            picker->Pick(eventPos[0], eventPos[1], 0, this->GetDefaultRenderer());

            qreal dpr = this->RenderWindowWidget->devicePixelRatioF();
            int height = this->RenderWindowWidget->height();
            QPoint globalPos = this->RenderWindowWidget->mapToGlobal(QPoint(eventPos[0] / dpr, height - eventPos[1] / dpr));

            emit signal_rightButtonUpEvent(globalPos, picker->GetActor());

            //QMenu menu;

            //if (picker->GetActor() != nullptr)
            //{
            //    menu.addAction("移动结果图位置");
            //}
            //else
            //{
            //    menu.addAction("添加标记点菜单");
            //}

            //QPoint globalPos;
            //if (this->RenderWindowWidget)
            //{
            //    qreal dpr = this->RenderWindowWidget->devicePixelRatioF();
            //    int height = this->RenderWindowWidget->height();
            //    globalPos = this->RenderWindowWidget->mapToGlobal(QPoint(eventPos[0] / dpr, height - eventPos[1] / dpr));
            //}
            //else
            //{
            //    globalPos = QCursor::pos();
            //}

            //menu.exec(globalPos);
            vtkInteractorStyleTrackballCamera::OnRightButtonUp();
        }
        void OnLeftButtonDown() override
        {
            if (m_interactionStyle == 0) // 左键按住是旋转
            {
                vtkInteractorStyleTrackballCamera::OnLeftButtonDown();
            }
            else if (m_interactionStyle == 1) // 左键按住是平移
            {
                vtkInteractorStyleTrackballCamera::OnMiddleButtonDown();
            }
        }

        void OnLeftButtonUp() override
        {
            if (m_interactionStyle == 0) // 左键按住是旋转
            {
                vtkInteractorStyleTrackballCamera::OnLeftButtonUp();
            }
            else if (m_interactionStyle == 1) // 左键按住是平移
            {
                vtkInteractorStyleTrackballCamera::OnMiddleButtonUp();
            }
        }


    signals:
        // 当右键释放时且点击在 actor 上时发射，参数为全局坐标
        void signal_rightButtonUpEvent(const QPoint& globalPos, vtkActor* pickedActor);

    protected:
        TDSPDefaultInteractorStyle() : RenderWindowWidget(nullptr) {}
        ~TDSPDefaultInteractorStyle() {}

        int m_interactionStyle{0}; //0 左键按住是旋转 1 左键按住是旋转

    private:
        QWidget* RenderWindowWidget;
    };

    //vtkStandardNewMacro(TDSPDefaultInteractorStyle);
}

#endif // TDSPDefaultInteractorStyle_H



//// MyInteractorStyle.h
//#ifndef MYINTERACTORSTYLE_H
//#define MYINTERACTORSTYLE_H
//
//#include <QObject>
//#include <vtkInteractorStyleTrackballCamera.h>
//#include <vtkObjectFactory.h>
//namespace pst
//{
//
//
//    // 注意继承顺序：QObject需要放在前面
//    class TDSPDefaultInteractorStyle : public QObject, public vtkInteractorStyleTrackballCamera
//    {
//        Q_OBJECT
//    public:
//        static TDSPDefaultInteractorStyle* New();
//        vtkTypeMacro(TDSPDefaultInteractorStyle, vtkInteractorStyleTrackballCamera);
//
//        // 用于设置用于计算全局坐标的 QWidget，一般为 QVTKWidget 或 QVTKOpenGLNativeWidget
//        void SetRenderWindowWidget(QWidget* widget) { this->RenderWindowWidget = widget; }
//        // 
//            // 重写VTK的事件处理方法，比如鼠标左键按下事件
//        virtual void OnLeftButtonDown() override
//        {
//            // 发射一个Qt信号
//            emit leftButtonClicked();
//
//            // 保持VTK的默认行为
//            this->vtkInteractorStyleTrackballCamera::OnLeftButtonDown();
//        }
//
//    signals:
//        // 定义你需要的Qt信号
//        void leftButtonClicked();
//    private:
//        QWidget* RenderWindowWidget;
//    };
//
//    //vtkStandardNewMacro(TDSPDefaultInteractorStyle);
//}
//
//#endif // MYINTERACTORSTYLE_H
